package ljx;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class login extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //设置响应格式
        response.setContentType("text/html");
        //接收请求中的参数
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        boolean is=false;
        Connection conn= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            conn = JdbcUtils.getConnection();
            String sql="select * from users where name=? and password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            rs=ps.executeQuery();
            if(rs.next()){
                is=true;
                response.sendRedirect("index2.jsp");
            }else{
                is=false;
                response.sendRedirect("error.jsp");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JdbcUtils.relesse(conn, ps, rs);
        PrintWriter out=response.getWriter();
        out.flush();
        out.close();
    }
}
